package tienich360.com.lib;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

public class Utils {
	public static final int BUFFER_SIZE = 256;
	public static Context cont;

	public static JSONObject getJSONFromUrlPost(String host, String data) {
		InputStream is = null;
		JSONObject jObj = null;
		String json = "";
		try {
			URL url = new URL(host);
			URLConnection conn = url.openConnection();
			conn.setDoOutput(true);
			conn.addRequestProperty("Content-Length", data.length() + "");
			OutputStreamWriter wr = new OutputStreamWriter(
					conn.getOutputStream());
			wr.write(data);
			wr.flush();
			is = conn.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "utf-8"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();
			json = sb.toString();
			jObj = new JSONObject(json);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
		return jObj;
	}

	public static String getDataFromUrlPost(String host, String data) {
		InputStream is = null;
		JSONObject jObj = null;
		String json = "";
		try {
			URL url = new URL(host);
			URLConnection conn = url.openConnection();
			conn.setDoOutput(true);
			conn.addRequestProperty("Content-Length", data.length() + "");
			OutputStreamWriter wr = new OutputStreamWriter(
					conn.getOutputStream());
			wr.write(data);
			wr.flush();
			is = conn.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "utf-8"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();
			json = sb.toString();
		} catch (Exception ex) {
			ex.printStackTrace();
			return "";
		}
		return json;
	}

	public static String getDataFromServer(String url) {
		HttpGet request = new HttpGet(url);
		try {
			HttpResponse response = new DefaultHttpClient().execute(request);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				return EntityUtils.toString(entity);
			}
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}
		return "";
	}

	// chuyen tieng viet co dau thanh tieng viet khog dau
	public static String toUnsignString(String str) {
		char[] chararray = str.toCharArray();
		for (int i = 0; i < chararray.length; i++) {
			int j;
			if ((j = tiengvietcodau.indexOf(chararray[i])) > -1)
				chararray[i] = tiengvietkhongdau.charAt(j);
		}
		return String.valueOf(chararray);
	}

	private final static String tiengvietcodau = "\341\340\u1EA3\343\u1EA1\u0103\u1EAF\u1EB1\u1EB3\u1EB5\u1EB7\342\u1EA5\u1EA7\u1EA9\u1EAB\u1EAD\351\350\u1EBB\u1EBD\u1EB9\352\u1EBF\u1EC1\u1EC3\u1EC5\u1EC7\355\354\u1EC9\u0129\u1ECB\363\362\u1ECF\365\u1ECD\364\u1ED1\u1ED3\u1ED5\u1ED7\u1ED9\u01A1\u1EDB\u1EDD\u1EDF\u1EE1\u1EE3\372\371\u1EE7\u0169\u1EE5\u01B0\u1EE9\u1EEB\u1EED\u1EEF\u1EF1\375\u1EF3\u1EF7\u1EF9\u1EF5\u0111\301\300\u1EA2\303\u1EA0\u0102\u1EAE\u1EB0\u1EB2\u1EB4\u1EB6\302\u1EA4\u1EA6\u1EA8\u1EAA\u1EAC\311\310\u1EBA\u1EBC\u1EB8\312\u1EBE\u1EC0\u1EC2\u1EC4\u1EC6\315\314\u1EC8\u0128\u1ECA\323\322\u1ECE\325\u1ECC\324\u1ED0\u1ED2\u1ED4\u1ED6\u1ED8\u01A0\u1EDA\u1EDC\u1EDE\u1EE0\u1EE2\332\331\u1EE6\u0168\u1EE4\u01AF\u1EE8\u1EEA\u1EEC\u1EEE\u1EF0\335\u1EF2\u1EF6\u1EF8\u1EF4\u0110";
	private final static String tiengvietkhongdau = "aaaaaaaaaaaaaaaaaeeeeeeeeeeeiiiiiooooooooooooooooouuuuuuuuuuuyyyyydAAAAAAAAAAAAAAAAAEEEEEEEEEEEIIIIIOOOOOOOOOOOOOOOOOUUUUUUUUUUUYYYYYD";

	// private TelephonyManager telephonyManager;
	// private ConnectivityManager connectivityManager;
	// ham kiem tra may co ket noi internet hay khong?
	public static boolean isConnected(TelephonyManager telephonyManager,
			ConnectivityManager connectivityManager) {
		if (telephonyManager == null || connectivityManager == null) {
			return false;
		}
		boolean roaming = telephonyManager.isNetworkRoaming();
		NetworkInfo info = connectivityManager.getActiveNetworkInfo();
		return info != null && info.isConnected() && !roaming;
	}

	public static boolean isConnected(Activity activity) {
		try {
			TelephonyManager telephonyManager = (TelephonyManager) activity
					.getSystemService(Context.TELEPHONY_SERVICE);
			ConnectivityManager connectivityManager = (ConnectivityManager) activity
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			if (telephonyManager == null || connectivityManager == null) {
				return false;
			}
			boolean roaming = telephonyManager.isNetworkRoaming();
			NetworkInfo info = connectivityManager.getActiveNetworkInfo();
			return info != null && info.isConnected() && !roaming;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	public static String getIMEI(TelephonyManager telephonyManager) {
		String emei = "";
		emei = telephonyManager.getDeviceId() + "";
		return emei;
	}

	// private static SharedPreferences mShareRefs = null;
	// private static String SUBID_KEY = "subid";

	public static void saveSubid(String subid, String SUBID_KEY, Activity ac) {
		try {
			SharedPreferences mShareRefs = ac.getSharedPreferences(SUBID_KEY,
					Activity.MODE_PRIVATE);
			SharedPreferences.Editor editor = mShareRefs.edit();
			editor.putString(SUBID_KEY, subid);
			editor.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void saveSubidString(String subid, String SUBID_KEY,
			Activity ac) {
		try {
			SharedPreferences mShareRefs = ac.getSharedPreferences(SUBID_KEY,
					Activity.MODE_PRIVATE);
			SharedPreferences.Editor editor = mShareRefs.edit();
			editor.putString(SUBID_KEY, subid);
			editor.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void saveSubidBoolean(Boolean subid, String SUBID_KEY,
			Activity ac) {
		try {
			SharedPreferences mShareRefs = ac.getSharedPreferences(SUBID_KEY,
					Activity.MODE_PRIVATE);
			SharedPreferences.Editor editor = mShareRefs.edit();
			editor.putBoolean(SUBID_KEY, subid);
			editor.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void saveSubidInt(int subid, String SUBID_KEY, Activity ac) {
		try {
			SharedPreferences mShareRefs = ac.getSharedPreferences(SUBID_KEY,
					Activity.MODE_PRIVATE);
			SharedPreferences.Editor editor = mShareRefs.edit();
			editor.putInt(SUBID_KEY, subid);
			editor.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getSubid(String SUBID_KEY, Activity ac) {
		String subID = "";
		try {
			SharedPreferences mShareRefs = ac.getSharedPreferences(SUBID_KEY,
					Activity.MODE_PRIVATE);
			subID = mShareRefs.getString(SUBID_KEY, "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return subID;
	}

	public static String getSubidString(String SUBID_KEY, Activity ac) {
		String subID = "";
		try {
			SharedPreferences mShareRefs = ac.getSharedPreferences(SUBID_KEY,
					Activity.MODE_PRIVATE);
			subID = mShareRefs.getString(SUBID_KEY, "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return subID;
	}

	public static boolean getSubidBoolean(String SUBID_KEY, Activity ac) {
		boolean subID = false;
		try {
			SharedPreferences mShareRefs = ac.getSharedPreferences(SUBID_KEY,
					Activity.MODE_PRIVATE);
			subID = mShareRefs.getBoolean(SUBID_KEY, false);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return subID;
	}

	public static int getSubidInt(String SUBID_KEY, Activity ac) {
		int subID = 0;
		try {
			SharedPreferences mShareRefs = ac.getSharedPreferences(SUBID_KEY,
					Activity.MODE_PRIVATE);
			subID = mShareRefs.getInt(SUBID_KEY, 0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return subID;
	}

	public static void showPopup(Activity activity, String mess,
			String butPosName) {
		AlertDialog.Builder ab = new AlertDialog.Builder(activity);
		ab.setMessage(mess);
		ab.setPositiveButton(butPosName, new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		ab.show();
	}

	public static void showPopup(Activity activity, String mess) {
		AlertDialog.Builder ab = new AlertDialog.Builder(activity);

		ab.setMessage(mess);
		ab.setPositiveButton("Đồng ý", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		ab.show();
	}

	public static void showPopup(Activity activity, String mess,
			String butPosName, DialogInterface.OnClickListener onclick,
			String butNegaName) {
		AlertDialog.Builder ab = new AlertDialog.Builder(activity);
		ab.setMessage(mess);
		ab.setPositiveButton(butPosName,
				(android.content.DialogInterface.OnClickListener) onclick);
		ab.setNegativeButton(butNegaName,
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		ab.show();
	}

	public static void showPopup(Activity activity, String mess,
			String butPosName, DialogInterface.OnClickListener onclick) {
		AlertDialog.Builder ab = new AlertDialog.Builder(activity);
		ab.setMessage(mess);
		ab.setPositiveButton(butPosName, onclick);
		ab.show();
	}

	// ////////////
	public static void showPopup(Activity activity, String mess,
			String butPosName, boolean iscancle) {
		AlertDialog.Builder ab = new AlertDialog.Builder(activity);
		ab.setCancelable(iscancle);
		ab.setMessage(mess);
		ab.setPositiveButton(butPosName, new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		ab.show();
	}

	public static void showPopup(Activity activity, String mess,
			boolean iscancle) {
		AlertDialog.Builder ab = new AlertDialog.Builder(activity);
		ab.setCancelable(iscancle);
		ab.setMessage(mess);
		ab.setPositiveButton("Đồng ý", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		ab.show();
	}

	public static void showPopup(Activity activity, String mess,
			String butPosName, DialogInterface.OnClickListener onclick,
			String butNegaName, boolean iscancle) {
		AlertDialog.Builder ab = new AlertDialog.Builder(activity);
		ab.setCancelable(iscancle);
		ab.setMessage(mess);
		ab.setPositiveButton(butPosName,
				(android.content.DialogInterface.OnClickListener) onclick);
		ab.setNegativeButton(butNegaName,
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		ab.show();
	}

	public static void showPopup(Activity activity, String mess,
			String butPosName, DialogInterface.OnClickListener onclick,
			boolean iscancle) {
		AlertDialog.Builder ab = new AlertDialog.Builder(activity);
		ab.setCancelable(iscancle);
		ab.setMessage(mess);
		ab.setPositiveButton(butPosName, onclick);
		ab.show();
	}

	public static long getTimeStampCurrent() {
		return System.currentTimeMillis() / 1000;
	}
}
